import * as uploadApi from '@/api/upload'

export const upload = (option) => {
    let fileName = option.file.name.split('.')
    let suffix = fileName[fileName.length - 1]
    uploadApi.getAliSign(suffix).then(res => {
        if (res.code === 200) {
            let key = res.data.dir
            uploadApi.upload(option.file, res.data.host, res.data.accessid, res.data.policy, res.data.signature, key).then(res => {
                if (res.status === 200) {
                    option.onSuccess(key)
                } else {
                    option.onError('上传失败')
                }
            })
        }
    })
}

export const uploadAndGetPath = (option) => {
    let fileName = option.file.name.split('.')
    let suffix = fileName[fileName.length - 1]
    uploadApi.getAliSign(suffix).then(res => {
        if (res.code === 200) {
            let key = res.data.dir
            uploadApi.upload(option.file, res.data.host, res.data.accessid, res.data.policy, res.data.signature, key).then(res => {
                console.log(res)
                if (res.status === 200) {
                    uploadApi.getImgPath(key).then(resp => {
                        let path = resp.data
                        let imgInfo = {
                            dic: key,
                            path: path
                        }
                        option.onSuccess(imgInfo)
                    })
                } else {
                    option.onError('上传失败')
                }
            })
        }
    })
}
export const uploadFileAndGetPath = (option) => {
    let fileName = option.file.name.split('.')
    let suffix = fileName[fileName.length - 1]
    uploadApi.getAliSign(suffix).then(res => {
        if (res.code === 200) {
            let key = res.data.dir
            uploadApi.upload(option.file, res.data.host, res.data.accessid, res.data.policy, res.data.signature, key).then(res => {
                if (res.status === 200) {
                    uploadApi.getFilePath(key).then(resp => {
                        let path = resp.data
                        let imgInfo = {
                            dic: key,
                            path: path
                        }
                        option.onSuccess(imgInfo)
                    })
                } else {
                    option.onError('上传失败')
                }
            })
        }
    })
}

